package com.nbstar.film.spider.engine;

import com.nbstar.film.spider.domain.FilmSpiderDomain;
import com.nbstar.film.spider.excel.SiChuanExcelReader;
import com.nbstar.film.spider.excel.SiChuanExcelWriter;
import com.nbstar.film.spider.pojo.DouBanFilmInfo;
import com.nbstar.film.spider.pojo.FilmExcelInfo;
import com.nbstar.film.spider.pojo.FilmInfo;
import com.nbstar.film.spider.pojo.MaoYanFilmInfo;
import com.nbstar.film.spider.spider.*;
import com.nbstar.film.spider.thread.ProxyThread;
import com.nbstar.film.spider.thread.SpiderRequestThread;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.CountDownLatch;

@Slf4j
@Data
public class SCSpiderEngine extends BaseSpiderEngine{
    private boolean isFilm = true;

    @Override
    public void executeDomain(FilmSpiderDomain filmSpiderDomain) throws Exception {
            //CountDownLatch await = new CountDownLatch(1);

            //SpiderRequestThread maoyanThread = new SpiderRequestThread(await, SpiderRequestThread.RequestType.MAOYAN, filmSpiderDomain.getRaw().getFilmName());
            //maoyanThread.start();

            FilmSpider doubanSpider = spiderMap.get(FilmSpiderDomain.SPIDER_FROM_DOUBAN);
            ((DoubanFilmSpider) doubanSpider).setRetry(1);
            DouBanFilmInfo doubanFilmInfo = null;


            doubanFilmInfo = (DouBanFilmInfo) ((DoubanFilmSpider) doubanSpider).runProxy(filmSpiderDomain.getRaw().getFilmName());
            log.debug(doubanFilmInfo.toString());

            FilmSpider imdbSpider = spiderMap.get(FilmSpiderDomain.SPIDER_FROM_IMDB);;
            ((ImdbSpider) imdbSpider).setRetry(0);
            FilmInfo imdbFilmInfo = null;

            if (doubanFilmInfo != null && doubanFilmInfo.getImdb() != null) {

                imdbFilmInfo = ((ImdbSpider) imdbSpider).run(doubanFilmInfo.getImdb());
                imdbFilmInfo.setCname(filmSpiderDomain.getRaw().getFilmName());
            }
            if (null == imdbFilmInfo) {
                log.debug(filmSpiderDomain.getRaw().getFilmName() + " : IMDB抓取数据失败!");
                imdbFilmInfo = new FilmInfo();
            } else {
                log.debug("IMDB:" + imdbFilmInfo.toString());
            }
            filmSpiderDomain.put(FilmSpiderDomain.SPIDER_FROM_IMDB, imdbFilmInfo);
            //下面开始写excel
            filmSpiderDomain.put(FilmSpiderDomain.SPIDER_FROM_DOUBAN, doubanFilmInfo);

            if (!isFilm) {
                FilmSpider tvCatSpider = spiderMap.get(FilmSpiderDomain.SPIDER_FROM_TVMAO_TV);;
                FilmInfo tvCatFilmInfo = ((TVCatSpider) tvCatSpider).run(filmSpiderDomain.getRaw().getFilmName());
                log.debug("TVCAT:" + tvCatFilmInfo);
                filmSpiderDomain.put(FilmSpiderDomain.SPIDER_FROM_TVMAO_TV, tvCatFilmInfo);
            }

            FilmSpider bkSpider = spiderMap.get(FilmSpiderDomain.SPIDER_FROM_BAIKE);
            FilmInfo bkInfo = ((BaseSpider) bkSpider).run(filmSpiderDomain.getRaw().getFilmName());
            filmSpiderDomain.put(FilmSpiderDomain.SPIDER_FROM_BAIKE, bkInfo);
            log.debug("BaiKe:" + bkInfo);

            FilmSpider maoyanSpider = spiderMap.get(FilmSpiderDomain.SPIDER_FROM_MAOYAN);;
            ((MaoYanSpider) maoyanSpider).setRetry(0);
            MaoYanFilmInfo maoYanFilmInfo = (MaoYanFilmInfo) ((MaoYanSpider) maoyanSpider).run(filmSpiderDomain.getRaw().getFilmName());
            //await.await();
            log.debug("MAOYAN:" + maoYanFilmInfo.toString());
            filmSpiderDomain.put(FilmSpiderDomain.SPIDER_FROM_MAOYAN, maoYanFilmInfo);
    }

    @Override
    public boolean checkDomainSucc(FilmSpiderDomain domain) {
        FilmExcelInfo filmExcelInfo = new FilmExcelInfo();
        DouBanFilmInfo douBanFilmInfo = (DouBanFilmInfo) domain.get(FilmSpiderDomain.SPIDER_FROM_DOUBAN);
        MaoYanFilmInfo maoyanFilmInfo = (MaoYanFilmInfo) domain.get(FilmSpiderDomain.SPIDER_FROM_MAOYAN);
        FilmInfo imdbFilmInfo = domain.get(FilmSpiderDomain.SPIDER_FROM_IMDB);
        FilmInfo tvCatFilmInfo = domain.get(FilmSpiderDomain.SPIDER_FROM_TVMAO_TV);
        FilmInfo bkFilmInfo = domain.get(FilmSpiderDomain.SPIDER_FROM_BAIKE);

        if (douBanFilmInfo != null &&
                maoyanFilmInfo != null ) {
            return douBanFilmInfo.isSuccess() && maoyanFilmInfo.isSuccess();
        }

        return  false;
    }

    @Override
    public void initEngine() {
        registSpider(FilmSpiderDomain.SPIDER_FROM_DOUBAN, (BaseSpider) SpiderFactory.create(FilmSpiderDomain.SPIDER_FROM_DOUBAN));
        registSpider(FilmSpiderDomain.SPIDER_FROM_IMDB, (BaseSpider) SpiderFactory.create(FilmSpiderDomain.SPIDER_FROM_IMDB));
        registSpider(FilmSpiderDomain.SPIDER_FROM_TVMAO_TV, (BaseSpider) SpiderFactory.create(FilmSpiderDomain.SPIDER_FROM_TVMAO_TV));
        registSpider(FilmSpiderDomain.SPIDER_FROM_MAOYAN, (BaseSpider) SpiderFactory.create(FilmSpiderDomain.SPIDER_FROM_MAOYAN));
        registSpider(FilmSpiderDomain.SPIDER_FROM_BAIKE, (BaseSpider) SpiderFactory.create(FilmSpiderDomain.SPIDER_FROM_BAIKE));
        //获取代理
        ProxyThread proxyThread = new ProxyThread();
        proxyThread.start();
        this.excelReader = new SiChuanExcelReader();
        this.excelWriter = new SiChuanExcelWriter(resultSheetName, resultExcelName);

        excelReader.setFilmSpiderQueue(filmSpiderQueue);
    }
}
